1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
use super::*;

/// A set of constraints on a value being valid
#[derive(Clone, Eq, PartialEq, Hash)]
pub enum Constraints {
    /// A valid set of constraints
    Consistent(Consistent),
    /// A contradiction
    Contradiction,
}

impl Constraints {
    /// Create a new constraint set with a single dependency
    #[inline]
    pub fn dependency(dep: ValId) -> Constraints {
        let mut result = Constraints::default();
        result.require(
            None,
            Some(dep),
            Relationship::EQ.with_usage(Usage::USED),
            true,
        );
        result
    }
    /// Try to require a constraint between two values, returning an error on inconsistency
    pub fn try_require(
        &mut self,
        left: Option<ValId>,
        right: Option<ValId>,
        relationship: Relationship,
        separating: bool,
    ) -> Result<bool, Error> {
        match self {
            Constraints::Consistent(c) => c.require(left, right, relationship, separating),
            Constraints::Contradiction => Err(Error::Contradiction),
        }
    }
    /// Require a rebased constraint on a value, returning an error on inconsistency
    pub fn try_rebase_require(
        &mut self,
        value: Option<ValId>,
        constraint: &Constraint,
        base: Option<&ValId>,
        separating: bool,
    ) -> Result<bool, Error> {
        match self {
            Constraints::Consistent(c) => c.rebase_require(value, constraint, base, separating),
            Constraints::Contradiction => Err(Error::Contradiction),
        }
    }
    /// Clear a constraint set
    #[inline]
    pub fn clear(&mut self) {
        match self {
            Constraints::Consistent(c) => c.clear(),
            Constraints::Contradiction => *self = NO_CONSTRAINTS,
        }
    }
    /// Check whether this constraint set is consistent
    #[inline]
    pub fn is_consistent(&self) -> bool {
        matches!(self, Constraints::Consistent(_))
    }
    /// Check whether this constraint set is a contradiction
    #[inline]
    pub fn is_contradiction(&self) -> bool {
        matches!(self, Constraints::Contradiction)
    }
    /// Take the rebase-union of this constraint set with another
    pub fn rebase_union(
        &mut self,
        other: &Constraints,
        base: Option<&ValId>,
        separating: bool,
    ) -> bool {
        use Constraints::*;
        //TODO: report the need for an &mut *self to appease the borrow checker
        if let (Consistent(this), Consistent(other)) = (&mut *self, other) {
            if let Ok(change) = this.rebase_union(other, base, separating) {
                return change;
            }
        }
        let change = !matches!(&self, Contradiction);
        *self = Contradiction;
        change
    }
    /// Take the union of this constraint set with another
    #[inline]
    pub fn union(&mut self, other: &Constraints, separating: bool) -> bool {
        self.rebase_union(other, None, separating)
    }
    /// Require a constraint between two values
    pub fn require(
        &mut self,
        left: Option<ValId>,
        right: Option<ValId>,
        relationship: Relationship,
        separating: bool,
    ) -> bool {
        //TODO: handle infinities, and such...
        if let Ok(change) = self.try_require(left, right, relationship, separating) {
            return change;
        }
        let change = self.is_consistent();
        *self = Constraints::Contradiction;
        change
    }
    /// Require a rebased constraint on a value, returning an error on inconsistency
    pub fn rebase_require(
        &mut self,
        value: Option<ValId>,
        constraint: &Constraint,
        base: Option<&ValId>,
        separating: bool,
    ) -> bool {
        if let Ok(change) = self.try_rebase_require(value, constraint, base, separating) {
            return change;
        }
        let change = self.is_consistent();
        *self = Constraints::Contradiction;
        change
    }
    /// Try to iterate over this constraint set
    pub fn try_iter(&self) -> Result<impl Iterator<Item = (&Option<ValId>, &Constraint)>, Error> {
        match self {
            Constraints::Consistent(c) => Ok(c.iter()),
            _ => Err(Error::Contradiction),
        }
    }
    /// Iterate over this constraint set, returning the empty iterator in the case of an inconsistent constraint-set
    pub fn iter(&self) -> impl Iterator<Item = (&Option<ValId>, &Constraint)> {
        match self {
            Constraints::Consistent(c) => c.iter(),
            _ => NO_CONSTRAINTS_CONSISTENT.iter(),
        }
    }
    /// Get the constraint of a symbol, if any
    pub fn constraint(&self, symbol: &Option<ValId>) -> &Constraint {
        match self {
            Constraints::Consistent(c) => c.constraint(symbol),
            _ => &NO_CONSTRAINT,
        }
    }
    /// Get the values which come before or at the same time as this value, according to this constraint-set
    pub fn prev(&self, value: &Option<ValId>) -> impl Iterator<Item = (&ValId, Relationship)> {
        self.constraint(value)
            .iter()
            .filter_map(|(value, relationship)| match relationship.variance() {
                Covariant | Invariant => value.as_ref().map(|value| (value, *relationship)),
                _ => None,
            })
    }
    /// Get the strict dependencies of this value, according to this constraint set
    pub fn deps(&self, value: &Option<ValId>) -> impl Iterator<Item = (&ValId, Usage)> {
        self.constraint(value)
            .iter()
            .filter_map(|(value, relationship)| {
                match (relationship.variance(), relationship.strict()) {
                    (Covariant, true) => value.as_ref().map(|value| (value, relationship.usage())),
                    _ => None,
                }
            })
    }
    /// Check this constraint set for cycles
    pub fn cycle_check(&mut self, arg: &SymbolId, result: &ValId) -> bool {
        match self {
            Constraints::Consistent(consistent) => {
                if let Ok(change) = consistent.cycle_check(result, |(value, _)| {
                    if value.fv().contains(arg) {
                        Some(value)
                    } else {
                        None
                    }
                }) {
                    return change;
                }
            }
            Constraints::Contradiction => return false,
        };
        *self = Constraints::Contradiction;
        true
    }
    /// Get the code for this constraint set
    pub fn code(&self) -> u64 {
        match self {
            Constraints::Consistent(c) => c.code(),
            Constraints::Contradiction => 0xC0,
        }
    }
    /// Compute the free variable set of this constraint set
    pub fn fv(&self) -> SymbolSet {
        match self {
            Constraints::Consistent(c) => c.fv(),
            Constraints::Contradiction => SymbolSet::default(),
        }
    }
}

impl BitAnd<&'_ Constraints> for Constraints {
    type Output = Constraints;
    fn bitand(mut self, other: &'_ Constraints) -> Constraints {
        self.union(other, false);
        self
    }
}

impl BitAnd<Constraints> for &'_ Constraints {
    type Output = Constraints;
    fn bitand(self, mut other: Constraints) -> Constraints {
        other.union(self, false);
        other
    }
}

impl BitAnd<&'_ Constraints> for &'_ Constraints {
    type Output = Constraints;
    fn bitand(self, other: &Constraints) -> Constraints {
        self.clone() & other
    }
}

impl BitAnd<Constraints> for Constraints {
    type Output = Constraints;
    fn bitand(self, other: Constraints) -> Constraints {
        self & &other
    }
}

impl Mul<&'_ Constraints> for Constraints {
    type Output = Constraints;
    fn mul(mut self, other: &'_ Constraints) -> Constraints {
        self.union(other, true);
        self
    }
}

impl Mul<Constraints> for &'_ Constraints {
    type Output = Constraints;
    fn mul(self, mut other: Constraints) -> Constraints {
        other.union(self, true);
        other
    }
}

impl Mul<&'_ Constraints> for &'_ Constraints {
    type Output = Constraints;
    fn mul(self, other: &Constraints) -> Constraints {
        self.clone() * other
    }
}

impl Mul<Constraints> for Constraints {
    type Output = Constraints;
    fn mul(self, other: Constraints) -> Constraints {
        self * &other
    }
}

impl Default for Constraints {
    #[inline]
    fn default() -> Constraints {
        NO_CONSTRAINTS
    }
}

impl Debug for Constraints {
    fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
        match self {
            Constraints::Consistent(c) => Debug::fmt(c, fmt),
            Constraints::Contradiction => write!(fmt, "{{!}}"),
        }
    }
}